package com.woniuxy.happylife.datastore.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.Queue;
import org.springframework.amqp.core.TopicExchange;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.HashMap;
import java.util.Map;

/**
 * rabbitmq配置类
 */
@Configuration
public class RabbitmqConfiguration {
    /**
     *   定义交换机
     */
    @Bean
    public TopicExchange exchangeTopic(){
        return new TopicExchange("exchangeTopic");
    }

    /**
     * 定义队列
     */
    @Bean
    public Queue queueRelease(){
//        return new Queue("queueRelease");
        //队列的额外参数
        Map<String, Object> arguments = new HashMap<>();
        //exchangeDelay 是一个死信交换机
        arguments.put("x-dead-letter-exchange","exchangeDelay");
        //delay 代表死信交换机的一个路由KEY
        arguments.put("x-dead-letter-routing-key","delay");
        return new Queue("queueRelease", true, false, false, arguments);
    }

    /**
     * 定义队列
     */
    @Bean
    public Queue queueDynamic(){
        //队列的额外参数
        Map<String, Object> arguments = new HashMap<>();
        //exchangeDelay 是一个死信交换机
        arguments.put("x-dead-letter-exchange","exchangeDelay");
        //delay 代表死信交换机的一个路由KEY
        arguments.put("x-dead-letter-routing-key","dynamicDelay");
        return new Queue("queueDynamic", true, false, false, arguments);
    }


    /**
     * 绑定队列到交换机
     */
    @Bean
    public Binding bindingRelease(TopicExchange exchangeTopic, Queue queueRelease){
        return BindingBuilder.bind(queueRelease).to(exchangeTopic).with("#.release.#");
    }
    /**
     * 绑定队列到交换机
     */
    @Bean
    public Binding bindingDynamic(TopicExchange exchangeTopic, Queue queueDynamic){
        return BindingBuilder.bind(queueDynamic).to(exchangeTopic).with("#.dynamic.#");
    }

    /**
     * 死信队列交换机
     * @return
     */
    @Bean
    public TopicExchange exchangeDelay(){
        TopicExchange exchangeDelay=new TopicExchange("exchangeDelay");
        return  exchangeDelay;
    }

    /**
     * 死信队列
     * @return
     */
    @Bean
    public Queue queueDelay(){
        Queue queueDelay=new Queue("queueDelay");
        return  queueDelay;
    }

    /**
     * 死信队列
     * @return
     */
    @Bean
    public Queue queueDynamicDelay(){
        Queue queueDynamicDelay=new Queue("queueDynamicDelay");
        return  queueDynamicDelay;
    }

    /**
     * 死信队列绑定交换机
     * @param queueDelay
     * @param exchangeDelay
     * @return
     */
    @Bean
    public Binding bindingQueueDelay(Queue queueDelay,TopicExchange exchangeDelay){
        return BindingBuilder.bind(queueDelay).to(exchangeDelay).with("#.delay.#");
    }
    /**
     * 死信队列绑定交换机
     * @param queueDynamicDelay
     * @param exchangeDelay
     * @return
     */
    @Bean
    public Binding bindingQueueDynamicDelay(Queue queueDynamicDelay,TopicExchange exchangeDelay){
        return BindingBuilder.bind(queueDynamicDelay).to(exchangeDelay).with("#.dynamicDelay.#");
    }
}
